tongfusoft.fieldset = function() {
}
tongfusoft.fieldset.prototype = {
	// 添加fieldset
	addFieldset : function() {
		$("#addBtn").click(function() {
			common.clean("addFieldsetForm");
			$('#dialogMode').modal('show');
		})

	},
	// 提交数据
	dataSubmit : function() {

		$.validator.setDefaults({
			submitHandler : function() {
				var initData = common.getData("addFieldsetForm");
				initData['profileId'] = $("#profileId").val();
				var url = 'fieldset/add';

				common.ajaxSubmitOnSuccess(initData, url, function(data) {

					if (data.state == 'success') {
						
						toastr["success"]('添加成功!');
						$("#dialogMode").modal('hide');
						
						window.location.href = common.getContextPath('manager/inputProfile/fieldsetMetadata/' + data.data.fieldsetId + "/add");
					} else if (data.code == 'repeat_error') {

						toastr["error"]('名称已经存在!');
					} else{
						toastr["error"]('保存失败!');
					}

				}, $("#oper").val())
			}
		});
		$("#addFieldsetForm").validate();

		$("#submitBtn").click(function() {
			$("#addFieldsetForm").submit();

		})
	},
	
	//获取fieldset列表数据
	fieldsetListTable : function() {
		common.post('fieldset/query', {
			"profileId" : $("#profileId").val()
		}, function(data) {
			$("#fieldsetData").html('');
			var fieldsetData='';
			if (data.state == 'success') {
				$.each(data.data, function(index, item) {
					var fieldsetHtml = '<tr>';
					fieldsetHtml = fieldsetHtml + '<td>'+item.fieldsetName +'</td>';
					fieldsetHtml = fieldsetHtml +  '<td>'
					fieldsetHtml = fieldsetHtml +       '<button fieldsetId="'+ item.fieldsetId +'"';
					fieldsetHtml = fieldsetHtml +       '" onclick="new tongfusoft.fieldset().deleteFieldset(this)" type="button" data-toggle="modal" class="detBtn btn btn-default btn-sm "><em class="fa fa-trash"></em></button>';
					fieldsetHtml = fieldsetHtml + '</td>';
					fieldsetHtml = fieldsetHtml + '</tr>';
						
					fieldsetData =fieldsetData+fieldsetHtml;
				});
			}
			$("#fieldsetListTable").html(fieldsetData);
		});
	},
	
	// 删除
	deleteFieldset: function(obj) {
			swal({
				title : "是否确认删除?",
				text : "",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "删除",
				cancelButtonText : "取消",
				closeOnConfirm : true
			}, function() {
				common.ajaxSubmitOnSuccess({
					"fieldsetId" : $(obj).attr("fieldsetId"),
					"profileId" : $("#profileId").val()
				}, 'fieldset/del', function(data) {

					if (data.state == 'success') {
						new tongfusoft.fieldset().fieldsetListTable();
					} else{
						toastr["error"]('删除失败!');
					}

				}, "del")
			});
			
	},
	
	savePage : function() {
		$("#saveBtn").click(function() {
			toastr["success"]('保存成功!');
			window.location.href = common.getContextPath('manager/inputProfile');
		})

	},
	returnBtn : function() {
		$("#returnBtn").click(function() {
			window.location.href = common.getContextPath('manager/inputProfile');
		})

	},
}

$(function() {
	var obj = new tongfusoft.fieldset();
	obj.addFieldset();
	obj.fieldsetListTable();
	obj.savePage();
	obj.returnBtn();
	obj.dataSubmit();
})